<?php
namespace admin\controllers;

use yii\web\Controller;
use yii\filters\AccessControl;
use admin\models\user\User;
use yii\data\Pagination;
use common\helpers\ErrorHelper;
use common\helpers\ShowJson;

class UserController extends Controller
{
    public $layout = "main2";
    
    public function behaviors()
    {
        return [
            'access' => [
                'class' => AccessControl::className(),
                'rules' => [
                    [
                        'actions' => ['list'],
                        'allow' => true,
                        'roles' => ['@'],
                    ],
                ],
            ],
        ];
    }
    
    // 用户列表
    public function actionList()
    {
        $request = \Yii::$app->request;
        $pageSize = $request->get('page_size', 50);
        
        $where = array();
        $userObj = User::find();
        $count = $userObj->where($where)->count();
        $pagination = new Pagination(['totalCount' => $count, 'pageSize' => $pageSize]);
        $userList = $userObj->where($where)->orderBy( 'id desc' )->offset( $pagination->offset )->limit( $pagination->limit )->asArray()->all();
        
        $data = array(
            'userList' => $userList
        );
        
        return $this->render("list", $data);
    }
    
    // 审核用户
    public function actionAuditpass()
    {
        $paramsError = ErrorHelper::get('paramsError');
        $request = \Yii::$app->request;
        $id = $request->get('id', 0);
        if (empty($id)) {
            ShowJson::showErrorJson($paramsError);
        }
        
        $userError = ErrorHelper::get('userError');
        $userObj = User::find()->where(['id' => $id]);
        if (empty($userObj)) {
            ShowJson::showErrorJson($userError);
        }
        $userObj->status = User::STATUS_ACTIVE;
        $userObj->save();
        
        ShowJson::showSuccessJson();
    }
    
    // 删除
    public function actionDelete()
    {
        $paramsError = ErrorHelper::get('paramsError');
        $request = \Yii::$app->request;
        $id = $request->get('id', 0);
        if (empty($id)) {
            ShowJson::showErrorJson($paramsError);
        }
        
        $userError = ErrorHelper::get('userError');
        $userObj = User::find()->where(['id' => $id]);
        if (empty($userObj)) {
            ShowJson::showErrorJson($userError);
        }
        $userObj->status = User::STATUS_DELETED;
        $userObj->save();
        
        ShowJson::showSuccessJson();
    }
}